package com.woniu.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.admin.mapper.TUserMapper;
import com.woniu.admin.pojo.TUser;
import com.woniu.admin.service.UserService;
import com.woniu.admin.vo.TUserVo;
import com.woniu.common.constants.Constants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TUserMapper userMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;   //redisTemplate是springboot提供的，用来操作redis的

    @Override
    public List<TUser> fineUserList(TUserVo userVo) {
        //先查缓存
        List<TUser> list = (List<TUser>) redisTemplate.opsForValue().get(Constants.USER_LIST_KEY);
        if (!Objects.isNull(list)) {
            return list;
        }
        //查数据库
        List<TUser> userlist = userMapper.userlist(userVo);
        //将数据存入缓存
        redisTemplate.opsForValue().set(Constants.USER_LIST_KEY, userlist);
        return userlist;
    }

    @Override
    public TUser login(TUser user) {
        return userMapper.login(user);
    }

    @Override
    public List<TUser> selectOneUser(TUser username) {
        return userMapper.selectOneUser(username);
    }

    @Override
    public void delete(Integer[] ids) {
        userMapper.deletes(ids);
    }

    @Override
    public void add(TUser user) {
        userMapper.insertUser(user);
    }

    @Override
    public void update(TUser user) {
        userMapper.updateUser(user);
    }
}
